﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Com.GoogleCode.Igda.Svn.Trunk.Anchor.Oden.HomeWork
{
    public class Cave
    {
        public  Cave()
        {
            const int max = 50;
            const int min = 5;
            int maxX = 50, maxY = 50;
            string input = null;

            do
            {// 迷路の横幅入力
                Console.WriteLine("迷路の横幅を入力してください");
                input = Console.ReadLine();
            } while (!(int.TryParse(input, out maxX) && maxX <= max && maxX >= min));

            do
            {// 迷路の縦幅入力
                Console.WriteLine("迷路の縦幅を入力してください");
                input = Console.ReadLine();
            } while (!(int.TryParse(input, out maxY) && maxY <= max && maxY >= min));

            char[,] cave = null;
            int root = 0;
            int x = 0, y = 0;

            // 口枠を作るよ
            cave = CreateRoot(maxX, maxY, cave);

            // 根の位置を決めるよ
            SetRootLocation(maxX, maxY, ref x, ref y, root);

            cave[x, y] = 'ス';

            do
            {
                // 枝の生えた場所をランダム決定するよ
                SetEdaLocation(ref x, ref y);

                // すべての芽を一回分伸ばすよ
                if (GoEda())
                {
                    break;
                }
            } while (GoEda());

            // 表示するよ
            Show(maxX, maxY, cave);
        }

        private static void Show(int maxX, int maxY, char[,] cave)
        {
            for (int i = 0; i < maxY; i++)
            {// 迷路を表示
                for (int j = 0; j < maxX; j++)
                {
                    Console.Write(cave[j, i]);
                }
                Console.WriteLine("");
            }
        }

        private static Boolean GoEda()
        {
            return true;
        }

        private static void SetEdaLocation(ref int x, ref int y)
        {
            int xEda = x, yEda = y;
            int me = 0;

            //me = random.Next(0, 4);
            if (me == 0)
            {// 上に進むよ

            }

            else if (me == 1)
            {// 右に進むよ

            }

            else if (me == 2)
            {// 下に進むよ
            }

            else if (me == 3)
            {// 左に進むよ
            }
        }

        /// <summary>
        /// 口枠を作る関数
        /// </summary>
        /// <param name="maxX"></param>
        /// <param name="maxY"></param>
        /// <param name="cave"></param>
        /// <returns></returns>
        private static char[,] CreateRoot(int maxX, int maxY, char[,] cave)
        {
            cave = new char[maxX, maxY];
            for (int i = 0; i < maxY; i++)
            {
                for (int j = 0; j < maxX; j++)
                {
                    cave[j, i] = '　';
                }
            }

            for (int i = 0; i < maxX; i++)
            {
                cave[i, 0] = '●';
                cave[i, maxY - 1] = '●';
            }

            for (int i = 0; i < maxY; i++)
            {
                cave[0, i] = '●';
                cave[maxX - 1, i] = '●';
            }

            return cave;
        }

        /// <summary>
        /// 根の位置を決めるよ
        /// </summary>
        private static void SetRootLocation(int maxX, int maxY, ref int x, ref int y, int root)
        {
            Random random = new Random();
            int rootX = 0;
            //int rootY = 0;

            if (random.Next(0, 2) == 1)
            {
                do
                {
                    rootX = random.Next(3, maxX - 2);
                    //rootY = random.Next(0, 2) ? 0 : 11 : maxY - 1;
                } while (rootX % 2 == 1);
            }
        }
    }
}
